package com.jason.game.online.home

import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.cby.android.olive.ext.dp
import com.cby.library.common.base.BaseStateFragment
import com.cby.library.common.ext.getColor
import com.cby.library.common.ext.observe
import com.cby.library.data.model.ModuleBean
import com.jason.game.online.R
import com.jason.game.online.home.adapter.HomeAdapter
import com.jason.game.online.home.view.HomeHeaderView

/**
 * @description: HomeFragment
 *
 * @author ChenBaoyang
 * @date 2021/10/26 16:25
 */
class HomeFragment : BaseStateFragment<HomeViewModel>() {

    private lateinit var toolbar: Toolbar
    private lateinit var refreshLayout: SwipeRefreshLayout
    private lateinit var recyclerView: RecyclerView
    private lateinit var homeAdapter: HomeAdapter
    private lateinit var headerView: HomeHeaderView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //加上这句话，menu才会显示出来
        setHasOptionsMenu(true)

    }

    override fun bindLiveData() {
        viewModel?.let { it ->
            observe(it.homeData) { data ->
                headerView.setBanner(data.data?.banner)
                headerView.setPoster(data.data?.poster)
                homeAdapter.data = data.data?.modules as MutableList<ModuleBean>
            }
        }
    }

    private fun hideIcon() {
        val c = Toolbar::class.java
        val mNavButtonView = c.getDeclaredField("mNavButtonView")
        mNavButtonView.isAccessible = true
        val btn: ImageButton = mNavButtonView.get(toolbar) as ImageButton
        btn.visibility = View.GONE
    }

    override fun initView(view: View) {
        toolbar = view.findViewById(R.id.toolbar)
        toolbar.run {
            hideIcon()
            setBackgroundColor(getColor(R.color.colorAccent))
            navigationIcon?.setVisible(false,false)
            title = "首页"
            setTitleMargin(20.dp,0,0,0)
            inflateMenu(R.menu.home_menu)
            setOnMenuItemClickListener {
                when (it.itemId) {
                    R.id.home_search -> {/*start(LoginActivity::class.java)*/}
                }
                true
            }
        }

        refreshLayout = view.findViewById(R.id.swipeRefreshLayout)
        recyclerView = view.findViewById(R.id.recyclerView)

        headerView = HomeHeaderView(activity!!)
        homeAdapter = HomeAdapter()
        homeAdapter.addHeaderView(headerView)
        recyclerView.adapter = homeAdapter
        recyclerView.layoutManager = LinearLayoutManager(activity)
    }

    override fun onDataInit() {
        viewModel?.loadData()
    }

    override fun getLayoutId() = R.layout.common_activity_recycler_view

    override fun getPageTarget() = refreshLayout

    override fun reload() {
        viewModel?.loadData()
    }


}